package database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import constants.GameTables;

import logic.AlreadyRegisteredException;

/** The class that updates the tables by means of inserts.
 * @author Team 1
 *
 */
public class Insert {

	/** Inserts a new player record in the specified table.
	 * @param name : The name of the player to be inserted.
	 * @param score : The number of points that the user has earned.
	 * @param table: The table in which the record is going to be stored.
	 */
	public static void insertRecord(String name, float score, GameTables table) {
		try {
			Connection conn = DataConnection.openConection();
			PreparedStatement s = conn.prepareStatement("Insert into " + table.toString() + " (nick, score) values(?, ?);");
			s.setString(1, name);
			s.setString(2, String.valueOf(score));
			s.executeUpdate();
			s.close();
			conn.close();
		} catch (SQLException e) {

		}
	}

	/** Inserts a new player to the login table.
	 * @param name : Name or nickname of the player to be inserted.
	 * @param pass : The password of the user to be inserted.
	 * @throws AlreadyRegisteredException : If the nick, the primary-key of the table, is already recorded into the 
	 * database it will throw a AlreadyRegisterdException alerting that the integrity of the database has been violated.
	 */
	public static void insertPlayer(String name, String pass) throws AlreadyRegisteredException {
		try {
			Connection conn = DataConnection.openConection();
			PreparedStatement s = conn.prepareStatement("Insert into LOGIN (nick, pass) values(?, ?);");
			s.setString(1, name);
			s.setString(2, pass);
			s.executeUpdate();
			s.close();
			conn.close();
		} catch (SQLException e) {
			throw new AlreadyRegisteredException();
		}
	}
}
